#!/usr/bin/python
# plot_apparent.py

import numpy
from matplotlib import use as useBackend
useBackend('Agg')
import matplotlib.pyplot as ppt


def cheat111():
    '''Cheat to clear the plot.'''
    ppt.subplot(311)
    ppt.plot([0,1],[1,2])
    ppt.subplot(111)


VariableNames = ['scale', 'appearance', 'outdir']

def plot(simpars, phypars, appearance):
    '''Shows a slice of GM_apparent's results.'''
    for i in simpars:
        cmd = "%s = simpars['%s']" % (i,i)
        exec cmd
    for i in phypars:
        cmd = "%s = phypars['%s']" % (i,i)
        exec cmd
    # Relevant distances
    dmax = int((scale + R0)/1000. + 0.001)
    bstring = 'Galactic latitude'
    dstring = 'Distance from sun, kpc'
    lstring = 'Galactic longitude'
    lims_ld = [0, dmax, 0, 180]
    lims_bd1 = [0, dmax, 0, 90]
    lims_bd2 = [0, dmax, 0, 90]
    # Aspect ratio
    asp_ld = 0.6*dmax/180.
    asp_bd = 0.6*dmax/90.

    # Plot slices for a bright and a dim population.
    brightstars = appearance[0]
    dimstars = appearance[-1]
    # Normalize, so that plot max values do not change if 'intervals' changes
    brightstars = brightstars/delMvVals[0]
    dimstars = dimstars/delMvVals[-1]
    # Bright stars
    cheat111()
    ppt.imshow(brightstars[:,:,0].T, origin='lower', extent=lims_ld, aspect=asp_ld)
    ppt.title('''Density of apparently bright stars
in the galactic plane''')
    ppt.colorbar(shrink=0.8)
    ppt.xlabel(dstring)
    ppt.ylabel(lstring)
    plotname = outdir+'AppBrightStarsldPlane.pdf'
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(brightstars[:,-1,:].T, origin='lower', extent=lims_bd1, aspect=asp_bd)
    ppt.title('''Density of apparently bright stars
away from the galactic center''')
    ppt.colorbar(shrink=0.8)
    ppt.xlabel(dstring)
    ppt.ylabel(bstring)
    plotname = outdir+'AppBrightStarsbdPlaneLeeward.pdf'
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(brightstars[:,0,:].T, origin='lower', extent=lims_bd1, aspect=asp_bd)
    ppt.title('''Density of apparently bright stars
towards the galactic center''')
    ppt.colorbar(shrink=0.8)
    ppt.xlabel(dstring)
    ppt.ylabel(bstring)
    plotname = outdir+'AppBrightStarsbdPlaneWindward.pdf'
    ppt.savefig(plotname)

    # Dim stars
    cheat111()
    ppt.imshow(dimstars[:,:,0].T, origin='lower', extent=lims_ld, aspect=asp_ld)
    ppt.title('''Density of apparently dim stars
in the galactic plane''')
    ppt.colorbar(shrink=0.8)
    ppt.xlabel(dstring)
    ppt.ylabel(lstring)
    plotname = outdir+'AppDimStarsldPlane.pdf'
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(dimstars[:,-1,:].T, origin='lower', extent=lims_bd1, aspect=asp_bd)
    ppt.title('''Density of apparently dim stars
away from the galactic center''')
    ppt.colorbar(shrink=0.8)
    ppt.xlabel(dstring)
    ppt.ylabel(bstring)
    plotname = outdir+'AppDimStarsbdPlaneLeeward.pdf'
    ppt.savefig(plotname)

    cheat111()
    ppt.imshow(dimstars[:,0,:].T, origin='lower', extent=lims_bd1, aspect=asp_bd)
    ppt.title('''Density of apparently dim stars
towards the galactic center''')
    ppt.colorbar(shrink=0.8)
    ppt.xlabel(dstring)
    ppt.ylabel(bstring)
    plotname = outdir+'AppDimStarsbdPlaneWindward.pdf'
    ppt.savefig(plotname)
